procedure swap(a,b:integer);
 var t :integer;
 begin
    t:=a;
	a:=b;
	b:=t;
 end;
function cmp(a,b,t:integer):boolean;
 begin
    if t = 1 then 
	  if a > b then exit(true)
       else
        exit(false)
    else
      if a < b then exit(true)
       else
        exit(false);
 end;
procedure selectkth(a:arrtype;froma,toa,n,k,typea:integer);
 var i,j:integer;
 begin
    if n = 1 then exit;
    i:=froma;
	j:=toa;
    repeat
       while cmp(a[i],a[k],typea) do inc(i);
       while cmp(a[k],a[j],typea) do dec(j);
       swap(a[i],a[j]);
    until i > j;
 end; 
procedure qsort(a:arrtype;froma,toa,n,typea:integer;);
 var k:integer;
 begin
    if n  <= 1 then exit;
    k:=trunc(froma+toa);
    seletekth(a,froma,toa,n,k,typea);
	qsort(a,froma,k-1,k-froma,typea);
	qsort(a,k+1,n,toa-k,typea);
 end;